package com.broadengate.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.broadengate.bean.NavagationInfo;
import com.broadengate.bean.RuleInfo;
import com.broadengate.bean.UserInfo;
import com.broadengate.dao.BaseDao;
import com.broadengate.dao.NavagationDao;
import com.broadengate.dao.UserDao;
import com.broadengate.util.ServiceException;
import com.broadengate.view.pageModel.RuleTreeGrid;

@Service("UserService")
public class UserService<T> extends BaseService<T>
{
    @Autowired
    private UserDao<T> userDao;
    
    @Autowired
    private NavagationDao<T> navagationDao;
    
    @Override
    public BaseDao<T> getDao()
    {
        // TODO Auto-generated method stub
        return userDao;
    }
    
    /**
     * 用户登录模块
     * @param info
     * @throws ServiceException [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public UserInfo login(UserInfo info) throws ServiceException
    {
        UserInfo resultInfo = userDao.login(info);
        if (resultInfo == null)
        {
            throw new ServiceException("登录失败!");
        }
        
        return resultInfo;
    }
    
    /**
     * 根据用户ID获取对应此用户的当前目录的下一级目录列表
     * @param userId
     * @param pid 父目录ID 默认为1
     * @return [参数说明]
     * 
     * @return List<RuleInfo> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public List<RuleInfo> getUserRuleList(int userId, String pid)
    {
        return userDao.getUserRuleList(userId, pid);
    }
    
    /**
     * 根据用户ID获取对应此用户的当前目录的下一级目录列表
     * @param userId
     * @param pid 父目录ID 默认为1
     * @return [参数说明]
     * 
     * @return List<RuleInfo> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public List<UserInfo> getUserList()
    {
        return userDao.getUserList();
    }
    
    /**
     * 根据用户ID查询此用户的目录权限
     * 首先获取目录的权限查询此用户此目录的权限，如果数据库中无此信息，则默认的为无权限
     * @return [参数说明]
     * 
     * @return RuleTreeGrid [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public RuleTreeGrid getRuleTreeGridByUserId(int userId)
    {
        //构造权限树对象，目录对象，权限对象
        RuleTreeGrid grid = new RuleTreeGrid();
        NavagationInfo nInfo = new NavagationInfo();
        RuleInfo rInfo = new RuleInfo();
        
        //先查询根目录信息
        nInfo = navagationDao.getNavagationInfoById("1");
        
        //根目录默认所有用户都具备此权限
        rInfo.setIsCanAdd("0");
        rInfo.setIsCanDelete("0");
        rInfo.setIsCanEdit("0");
        rInfo.setIsCanView("0");
        rInfo.setUserId(userId);
        this.setNavagationRule(grid, nInfo, rInfo);
        this.setLowerRuleTreeGrid(grid, userId);
        return grid;
    }
    
    /**
     * 根据目录ID查询目录信息
     * <功能详细描述>
     * @param id
     * @return [参数说明]
     * 
     * @return NavagationTreeInfo [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public NavagationInfo getNavationById(String id)
    {
        return null;
    }
    
    /**
     * 根据父目录ID查询所有子目录信息
     * @param pid
     * @return [参数说明]
     * 
     * @return List<NavagationTreeInfo> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public List<NavagationInfo> getLowerNavationByPid(String pid)
    {
        return null;
    }
    
    private void setNavagationRule(RuleTreeGrid grid, NavagationInfo nInfo, RuleInfo rInfo)
    {
        grid.setRuleName(nInfo.getText());
        grid.setTreeId(nInfo.getId());
        grid.setIsCanAdd(rInfo.getIsCanAdd());
        grid.setIsCanDelete(rInfo.getIsCanDelete());
        grid.setIsCanEdit(rInfo.getIsCanEdit());
        grid.setIsCanView(rInfo.getIsCanView());
        grid.setUserRuleId(rInfo.getUserRuleId());
        grid.setURL(nInfo.getUrl());
        grid.setUserId(rInfo.getUserId());
        
    }
    
    private void setLowerRuleTreeGrid(RuleTreeGrid targetGrid, int userId)
    {
        
        RuleInfo rInfo = new RuleInfo();
        
        //获取当前目录的所有子目录
        List<NavagationInfo> nList = navagationDao.getChildNavationInfoByPid(targetGrid.getTreeId());
        
        //遍历子目录，查看其所有子目录
        List<RuleTreeGrid> children = new ArrayList<RuleTreeGrid>();
        if (nList != null && nList.size() > 0)
        {
            for (NavagationInfo navagationInfo : nList)
            {
                RuleTreeGrid rtg = new RuleTreeGrid();
                //查询此用户的此目录权限
                rInfo = userDao.getUserRuleInfo(userId, navagationInfo.getId());
                //如果为空，则设置一个默认的权限给
                if (rInfo == null)
                {
                    rInfo = new RuleInfo();
                    rInfo.setUserId(userId);
                }
                setNavagationRule(rtg, navagationInfo, rInfo);
                children.add(rtg);
            }
        }
        targetGrid.setChildren(children);
        
        for (RuleTreeGrid ruleTreeGrid : children)
        {
            if (hasChildNavagation(ruleTreeGrid.getTreeId()))
            {
                setLowerRuleTreeGrid(ruleTreeGrid, userId);
            }
        }
        
    }
    
    private boolean hasChildNavagation(String pid)
    {
        List<NavagationInfo> nList = navagationDao.getChildNavationInfoByPid(pid);
        if (nList != null && nList.size() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void editUserRule(RuleInfo info) throws SQLException
    {
        //先判断是否存在此条权限记录，如果有，则直接修改，如果没有，则新增
        RuleInfo tmp = userDao.getUserRuleInfo(info.getUserId(), info.getTreeId());
        
        if (tmp == null)
        {
            userDao.insertUserRule(info);
        }
        else
        {
            userDao.editUserRule(info);
        }
    }
    
    /**
     * 根据用户ID获取用户名称
     * @param employeeId
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public String getEmployeeNameById(int employeeId)
    {
        return userDao.getEmployeeNameById(employeeId);
    }
}
